//
//  CZComposeController.swift
//  SinaWeibo
//
//  Created by 浩记 on 16/3/20.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class CZComposeController: UIViewController ,UITextViewDelegate{

    //微博长度文本的text值,最大长度
    let statusMaxLength = 20
    
//    let userLogin : Bool = false
    
//    override func loadView() {
    
//        view = userLogin == false ? view : UIView()
//        view = UIView()
//        if userLogin == false {
//            view = UIView()
//        }else{
//            super.loadView()
//        }
//    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "发微博"
        view.backgroundColor = UIColor.whiteColor()
        //展现UI
        prepareUI()
    }
    //MARK:- UI
    func prepareUI () {
        setUpNavigationItem()
        setUpLengthTipLabel()
    }
    /**
     设置导航栏控件
     */
    func setUpNavigationItem () {
        setUpBarButonItem()
        setUpTextView()
        
    }
    /**
     设置微博长度提示label
     */
    func setUpLengthTipLabel() {
        //添加
        view.addSubview(textLengthTipLabel)
        textLengthTipLabel.text = String(statusMaxLength)//文本没有文字时显示可以输入的文字长度
        textLengthTipLabel.sizeToFit()
        //添加约束,使用系统
        textLengthTipLabel.translatesAutoresizingMaskIntoConstraints = false
//        print("长度")
        view.addConstraint(NSLayoutConstraint(item: textLengthTipLabel, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: -4))
        
        view.addConstraint(NSLayoutConstraint(item: textLengthTipLabel, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -4))
        
        
    }
    /**
     设置导航栏按钮
     */
    func setUpBarButonItem () {
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "close")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发送", style: UIBarButtonItemStyle.Plain, target: self, action: "send")
        //让发送按钮不能用
        navigationItem.rightBarButtonItem?.enabled = false
    }
    func setUpTextView(){
        view.addSubview(textView)
//        textView.frame = view.frame
        textView.backgroundColor = UIColor.whiteColor()
        textView.scrollEnabled = false
        
        textView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: textView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 5))
        view.addConstraint(NSLayoutConstraint(item: textView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 5))
        
    }
    //MARK:- 点击按钮事件
    func send () {
        print("\(textView.text)")
    }
    func close () {
        print("quxiao")
        dismissViewControllerAnimated(true, completion: nil)
    }
    //MARK:- 代理回调
    func textViewDidChange(textView: UITextView) {
        //有文字时右边rightBarButtonItem可以点击
        navigationItem.rightBarButtonItem?.enabled = textView.hasText()
        
    }
    
    //MARK:- 懒加载
    private lazy var textView : CZPlaceHolderTextView = {
        let tv = CZPlaceHolderTextView()
        tv.alwaysBounceVertical = true
        tv.font = UIFont.systemFontOfSize(18)
        tv.placeholderString = "分享新鲜事..."
        tv.delegate = self
        return tv
    }()
    /// 发微博文字长度提示label
    private lazy var textLengthTipLabel : UILabel = UILabel(textColor: UIColor.lightGrayColor(), font: 11)
    
    
    
    
    
    
    deinit{
        print("清除")
    }
}
